﻿/*
Adapted from AAPlus library by PJ Naughter
Permission was granted to convert the library to C#

This source is modified from PJ Naugters AA+ lib
(http://www.naughter.com/aa.html)
and therefore is covered under his copyright license, which
is as follows:

Purpose: Implementation for the algorithms which obtain the heliocentric position of Uranus
Created: PJN / 29-12-2003
History: None

Copyright (c) 2003 - 2009 by PJ Naughter (Web: www.naughter.com, Email: pjna@naughter.com)

All rights reserved.

Copyright / Usage Details:

You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise) 
when your product is released in binary form. You are allowed to modify the source code in any way you want 
except you cannot modify the copyright details at the top of each module. If you want to distribute source 
code with your application, then you are only allowed to distribute versions released by the author. This is 
to maintain a single distribution point for the source code. 

*/


using System;

namespace AAPlus
{
    public static class Mars
    {
        
static readonly VSOP87Coefficient[] g_L0MarsCoefficients =
{ 
  new VSOP87Coefficient( 620347712,  0,          0 ),
  new VSOP87Coefficient( 18656368,   5.05037100, 3340.61242670 ),
  new VSOP87Coefficient( 1108217,    5.4009984,  6681.2248534 ),
  new VSOP87Coefficient( 91798,      5.75479,    10021.83728 ),
  new VSOP87Coefficient( 27745,      5.97050,    3.52312 ),
  new VSOP87Coefficient( 12316,      0.84956,    2810.92146 ),
  new VSOP87Coefficient( 10610,      2.93959,    2281.23050 ),
  new VSOP87Coefficient( 8927,       4.1570,     0.0173 ),
  new VSOP87Coefficient( 8716,       6.1101,     13362.4497 ),
  new VSOP87Coefficient( 7775,       3.3397,     5621.8429 ),
  new VSOP87Coefficient( 6798,       0.3646,     398.1490 ),
  new VSOP87Coefficient( 4161,       0.2281,     2942.4634 ),
  new VSOP87Coefficient( 3575,       1.6619,     2544.3144 ),
  new VSOP87Coefficient( 3075,       0.8570,     191.4483 ),
  new VSOP87Coefficient( 2938,       6.0789,     0.0673 ),
  new VSOP87Coefficient( 2628,       0.6481,     3337.0893 ),
  new VSOP87Coefficient( 2580,       0.0300,     3344.1355 ),
  new VSOP87Coefficient( 2389,       5.0390,     796.2980 ),
  new VSOP87Coefficient( 1799,       0.6563,     529.6910 ),
  new VSOP87Coefficient( 1546,       2.9158,     1751.5395 ),
  new VSOP87Coefficient( 1528,       1.1498,     6151.5339 ),
  new VSOP87Coefficient( 1286,       3.0680,     2146.1654 ),
  new VSOP87Coefficient( 1264,       3.6228,     5092.1520 ),
  new VSOP87Coefficient( 1025,       3.6933,     8962.4553 ),
  new VSOP87Coefficient( 892,        0.183,      16703.062 ),
  new VSOP87Coefficient( 859,        2.401,      2914.014 ),
  new VSOP87Coefficient( 833,        4.495,      3340.630 ),
  new VSOP87Coefficient( 833,        2.464,      3340.595 ),
  new VSOP87Coefficient( 749,        3.822,      155.420 ),
  new VSOP87Coefficient( 724,        0.675,      3738.761 ),
  new VSOP87Coefficient( 713,        3.663,      1059.382 ),
  new VSOP87Coefficient( 655,        0.489,      3127.313 ),
  new VSOP87Coefficient( 636,        2.922,      8432.764 ),
  new VSOP87Coefficient( 553,        4.475,      1748.016 ),
  new VSOP87Coefficient( 550,        3.810,      0.980 ),
  new VSOP87Coefficient( 472,        3.625,      1194.447 ),
  new VSOP87Coefficient( 426,        0.554,      6283.076 ),
  new VSOP87Coefficient( 415,        0.497,      213.299 ),
  new VSOP87Coefficient( 312,        0.999,      6677.702 ),
  new VSOP87Coefficient( 307,        0.381,      6684.748 ),
  new VSOP87Coefficient( 302,        4.486,      3532.061 ),
  new VSOP87Coefficient( 299,        2.783,      6254.627 ),
  new VSOP87Coefficient( 293,        4.221,      20.775 ),
  new VSOP87Coefficient( 284,        5.769,      3149.164 ),
  new VSOP87Coefficient( 281,        5.882,      1349.867 ),
  new VSOP87Coefficient( 274,        0.542,      3340.545 ),
  new VSOP87Coefficient( 274,        0.134,      3340.680 ),
  new VSOP87Coefficient( 239,        5.372,      4136.910 ),
  new VSOP87Coefficient( 236,        5.755,      3333.499 ),
  new VSOP87Coefficient( 231,        1.282,      3870.303 ),
  new VSOP87Coefficient( 221,        3.505,      382.897 ),
  new VSOP87Coefficient( 204,        2.821,      1221.849 ),
  new VSOP87Coefficient( 193,        3.357,      3.590 ),
  new VSOP87Coefficient( 189,        1.491,      9492.146 ),
  new VSOP87Coefficient( 179,        1.006,      951.718 ),
  new VSOP87Coefficient( 174,        2.414,      553.569 ),
  new VSOP87Coefficient( 172,        0.439,      5486.778 ),
  new VSOP87Coefficient( 160,        3.949,      4562.461 ),
  new VSOP87Coefficient( 144,        1.419,      135.065 ),
  new VSOP87Coefficient( 140,        3.326,      2700.715 ),
  new VSOP87Coefficient( 138,        4.301,      7.114 ),
  new VSOP87Coefficient( 131,        4.045,      12303.068 ),
  new VSOP87Coefficient( 128,        2.208,      1592.596 ),
  new VSOP87Coefficient( 128,        1.807,      5088.629 ),
  new VSOP87Coefficient( 117,        3.128,      7903.073 ),
  new VSOP87Coefficient( 113,        3.701,      1589.073 ),
  new VSOP87Coefficient( 110,        1.052,      242.729 ),
  new VSOP87Coefficient( 105,        0.785,      8827.390 ),
  new VSOP87Coefficient( 100,        3.243,      11773.377 )
};

static readonly VSOP87Coefficient[]  g_L1MarsCoefficients =
{ 
  new VSOP87Coefficient( 334085627474.0, 0,          0 ),
  new VSOP87Coefficient( 1458227,        3.6042605,  3340.6124267 ),
  new VSOP87Coefficient( 164901,         3.926313,   6681.224853 ),
  new VSOP87Coefficient( 19963,          4.26594,    10021.83728 ),
  new VSOP87Coefficient( 3452,           4.7321,     3.5231 ),
  new VSOP87Coefficient( 2485,           4.6128,     13362.4497 ),
  new VSOP87Coefficient( 842,            4.459,      2281.230 ),
  new VSOP87Coefficient( 538,            5.016,      398.149 ),
  new VSOP87Coefficient( 521,            4.994,      3344.136 ),
  new VSOP87Coefficient( 433,            2.561,      191.448 ),
  new VSOP87Coefficient( 430,            5.316,      155.420 ),
  new VSOP87Coefficient( 382,            3.539,      796.298 ),
  new VSOP87Coefficient( 314,            4.963,      16703.062 ),
  new VSOP87Coefficient( 283,            3.160,      2544.314 ),
  new VSOP87Coefficient( 206,            4.569,      2146.165 ),
  new VSOP87Coefficient( 169,            1.329,      3337.089 ),
  new VSOP87Coefficient( 158,            4.185,      1751.540 ),
  new VSOP87Coefficient( 134,            2.233,      0.980 ),
  new VSOP87Coefficient( 134,            5.974,      1748.016 ),
  new VSOP87Coefficient( 118,            6.024,      6151.534 ),
  new VSOP87Coefficient( 117,            2.213,      1059.382 ),
  new VSOP87Coefficient( 114,            2.129,      1194.447 ),
  new VSOP87Coefficient( 114,            5.428,      3738.761 ),
  new VSOP87Coefficient( 91,             1.10,       1349.87 ),
  new VSOP87Coefficient( 85,             3.91,       553.57 ),
  new VSOP87Coefficient( 83,             5.30,       6684.75 ),
  new VSOP87Coefficient( 81,             4.43,       529.69 ),
  new VSOP87Coefficient( 80,             2.25,       8962.46 ),
  new VSOP87Coefficient( 73,             2.50,       951.72 ),
  new VSOP87Coefficient( 73,             5.84,       242.73 ),
  new VSOP87Coefficient( 71,             3.86,       2914.01 ),
  new VSOP87Coefficient( 68,             5.02,       382.90 ),
  new VSOP87Coefficient( 65,             1.02,       3340.60 ),
  new VSOP87Coefficient( 65,             3.05,       3340.63 ),
  new VSOP87Coefficient( 62,             4.15,       3149.16 ),
  new VSOP87Coefficient( 57,             3.89,       4136.91 ),
  new VSOP87Coefficient( 48,             4.87,       213.30 ),
  new VSOP87Coefficient( 48,             1.18,       3333.50 ),
  new VSOP87Coefficient( 47,             1.31,       3185.19 ),
  new VSOP87Coefficient( 41,             0.71,       1592.60 ),
  new VSOP87Coefficient( 40,             2.73,       7.11 ),
  new VSOP87Coefficient( 40,             5.32,       20043.67 ),
  new VSOP87Coefficient( 33,             5.41,       6283.08 ),
  new VSOP87Coefficient( 28,             0.05,       9492.15 ),
  new VSOP87Coefficient( 27,             3.89,       1221.85 ),
  new VSOP87Coefficient( 27,             5.11,       2700.72 )
};

static readonly VSOP87Coefficient[]  g_L2MarsCoefficients =
{ 
  new VSOP87Coefficient( 58016,  2.04979,  3340.61243 ),
  new VSOP87Coefficient( 54188,  0,        0 ),
  new VSOP87Coefficient( 13908,  2.45742,  6681.22485 ),
  new VSOP87Coefficient( 2465,   2.8000,   10021.8373 ),
  new VSOP87Coefficient( 398,    3.141,    13362.450 ),
  new VSOP87Coefficient( 222,    3.194,    3.523 ),
  new VSOP87Coefficient( 121,    0.543,    155.420 ),
  new VSOP87Coefficient( 62,     3.49,     16703.06 ),
  new VSOP87Coefficient( 54,     3.54,     3344.14 ),
  new VSOP87Coefficient( 34,     6.00,     2281.23 ),
  new VSOP87Coefficient( 32,     4.14,     191.45 ),
  new VSOP87Coefficient( 30,     2.00,     796.30 ),
  new VSOP87Coefficient( 23,     4.33,     242.73 ),
  new VSOP87Coefficient( 22,     3.45,     398.15 ),
  new VSOP87Coefficient( 20,     5.42,     553.57 ),
  new VSOP87Coefficient( 16,     0.66,     0.98 ),
  new VSOP87Coefficient( 16,     6.11,     2146.17 ),
  new VSOP87Coefficient( 16,     1.22,     1748.02 ),
  new VSOP87Coefficient( 15,     6.10,     3185.19 ),
  new VSOP87Coefficient( 14,     4.02,     951.72 ),
  new VSOP87Coefficient( 14,     2.62,     1349.87 ),
  new VSOP87Coefficient( 13,     0.60,     1194.45 ),
  new VSOP87Coefficient( 12,     3.86,     6684.75 ),
  new VSOP87Coefficient( 11,     4.72,     2544.31 ),
  new VSOP87Coefficient( 10,     0.25,     382.90 ),
  new VSOP87Coefficient( 9,      0.68,     1059.38 ),
  new VSOP87Coefficient( 9,      3.83,     20043.67 ),
  new VSOP87Coefficient( 9,      3.88,     3738.76 ),
  new VSOP87Coefficient( 8,      5.46,     1751.54 ),
  new VSOP87Coefficient( 7,      2.58,     3149.16 ),
  new VSOP87Coefficient( 7,      2.38,     4136.91 ),
  new VSOP87Coefficient( 6,      5.48,     1592.60 ),
  new VSOP87Coefficient( 6,      2.34,     3097.88 )
 
};

static readonly VSOP87Coefficient[]  g_L3MarsCoefficients =
{ 
  new VSOP87Coefficient( 1482, 0.4443, 3340.6124 ),
  new VSOP87Coefficient( 662,  0.885,  6681.225 ),
  new VSOP87Coefficient( 188,  1.288,  10021.837 ),
  new VSOP87Coefficient( 41,   1.65,   13362.45 ),
  new VSOP87Coefficient( 26,   0,      0 ),
  new VSOP87Coefficient( 23,   2.05,   155.42 ),
  new VSOP87Coefficient( 10,   1.58,   3.52 ),
  new VSOP87Coefficient( 8,    2.00,   16703.06 ),
  new VSOP87Coefficient( 5,    2.82,   242.73 ),
  new VSOP87Coefficient( 4,    2.02,   3344.14 ),
  new VSOP87Coefficient( 3,    4.59,   3185.19 ),
  new VSOP87Coefficient( 3,    0.65,   553.57 )
};

static readonly VSOP87Coefficient[]  g_L4MarsCoefficients =
{ 
  new VSOP87Coefficient( 114,  3.1416, 0 ),
  new VSOP87Coefficient( 29,   5.64,   6681.22 ),
  new VSOP87Coefficient( 24,   5.14,   3340.61 ),
  new VSOP87Coefficient( 11,   6.03,   10021.84 ),
  new VSOP87Coefficient( 3,    0.13,   13362.45 ),
  new VSOP87Coefficient( 3,    3.56,   155.42 ),
  new VSOP87Coefficient( 1,    0.49,   16703.06 ),
  new VSOP87Coefficient( 1,    1.32,   242.73 )
};

static readonly VSOP87Coefficient[]  g_L5MarsCoefficients =
{ 
  new VSOP87Coefficient( 1,  3.14, 0 ),
  new VSOP87Coefficient( 1,  4.04, 6681.22 )
};


static readonly VSOP87Coefficient[]  g_B0MarsCoefficients =
{ 
  new VSOP87Coefficient( 3197135,  3.7683204,  3340.6124267 ),
  new VSOP87Coefficient( 298033,   4.106170,   6681.224853 ),
  new VSOP87Coefficient( 289105,   0,          0 ),
  new VSOP87Coefficient( 31366,    4.44651,    10021.83728 ),
  new VSOP87Coefficient( 3484,     4.7881,     13362.4497 ),
  new VSOP87Coefficient( 443,      5.026,      3344.136 ),
  new VSOP87Coefficient( 443,      5.652,      3337.089 ),
  new VSOP87Coefficient( 399,      5.131,      16703.062 ),
  new VSOP87Coefficient( 293,      3.793,      2281.230 ),
  new VSOP87Coefficient( 182,      6.136,      6151.534 ),
  new VSOP87Coefficient( 163,      4.264,      529.691 ),
  new VSOP87Coefficient( 160,      2.232,      1059.382 ),
  new VSOP87Coefficient( 149,      2.165,      5621.843 ),
  new VSOP87Coefficient( 143,      1.182,      3340.595 ),
  new VSOP87Coefficient( 143,      3.213,      3340.630 ),
  new VSOP87Coefficient( 139,      2.418,      8962.455 )
};

static readonly VSOP87Coefficient[]  g_B1MarsCoefficients =
{ 
  new VSOP87Coefficient( 350069, 5.368478, 3340.612427 ),
  new VSOP87Coefficient( 14116,  3.14159,  0 ),
  new VSOP87Coefficient( 9671,   5.4788,   6681.2249 ),
  new VSOP87Coefficient( 1472,   3.2021,   10021.8373 ),
  new VSOP87Coefficient( 426,    3.408,    13362.450 ),
  new VSOP87Coefficient( 102,    0.776,    3337.089 ),
  new VSOP87Coefficient( 79,     3.72,     16703.06 ),
  new VSOP87Coefficient( 33,     3.46,     5621.84 ),
  new VSOP87Coefficient( 26,     2.48,     2281.23 )
};

static readonly VSOP87Coefficient[]  g_B2MarsCoefficients =
{ 
  new VSOP87Coefficient( 16727,  0.60221,  3340.61243 ),
  new VSOP87Coefficient( 4987,   4.1416,   0 ),
  new VSOP87Coefficient( 302,    3.559,    6681.225 ),
  new VSOP87Coefficient( 26,     1.90,     13362.45 ),
  new VSOP87Coefficient( 21,     0.92,     10021.84 ),
  new VSOP87Coefficient( 12,     2.24,     3337.09 ),
  new VSOP87Coefficient( 8,      2.25,     16703.06 )
};

static readonly VSOP87Coefficient[]  g_B3MarsCoefficients =
{ 
  new VSOP87Coefficient( 607,  1.981,  3340.612 ),
  new VSOP87Coefficient( 43,   0,      0 ),
  new VSOP87Coefficient( 14,   1.80,   6681.22 ),
  new VSOP87Coefficient( 3,    3.45,   10021.84 )
};

static readonly VSOP87Coefficient[]  g_B4MarsCoefficients =
{ 
  new VSOP87Coefficient( 13, 0,    0 ),
  new VSOP87Coefficient( 11, 3.46, 3340.61 ),
  new VSOP87Coefficient( 1,  0.50, 6681.22 )
};


static readonly VSOP87Coefficient[]  g_R0MarsCoefficients =
{ 
  new VSOP87Coefficient( 153033488,  0,          0 ),
  new VSOP87Coefficient( 14184953,   3.47971284, 3340.61242670 ),
  new VSOP87Coefficient( 660776,     3.817834,   6681.224853 ),
  new VSOP87Coefficient( 46179,      4.15595,    10021.83728 ),
  new VSOP87Coefficient( 8110,       5.5596,     2810.9215 ),
  new VSOP87Coefficient( 7485,       1.7724,     5621.8429 ),
  new VSOP87Coefficient( 5523,       1.3644,     2281.2305 ),
  new VSOP87Coefficient( 3825,       4.4941,     13362.4497 ),
  new VSOP87Coefficient( 2484,       4.9255,     2942.4634 ),
  new VSOP87Coefficient( 2307,       0.0908,     2544.3144 ),
  new VSOP87Coefficient( 1999,       5.3606,     3337.0893 ),
  new VSOP87Coefficient( 1960,       4.7425,     3344.1355 ),
  new VSOP87Coefficient( 1167,       2.1126,     5092.1520 ),
  new VSOP87Coefficient( 1103,       5.0091,     398.1490 ),
  new VSOP87Coefficient( 992,        5.839,      6151.534 ),
  new VSOP87Coefficient( 899,        4.408,      529.691 ),
  new VSOP87Coefficient( 807,        2.102,      1059.382 ),
  new VSOP87Coefficient( 798,        3.448,      796.298 ),
  new VSOP87Coefficient( 741,        1.499,      2146.165 ),
  new VSOP87Coefficient( 726,        1.245,      8432.764 ),
  new VSOP87Coefficient( 692,        2.134,      8962.455 ),
  new VSOP87Coefficient( 633,        0.894,      3340.595 ),
  new VSOP87Coefficient( 633,        2.924,      3340.630 ),
  new VSOP87Coefficient( 630,        1.287,      1751.540 ),
  new VSOP87Coefficient( 574,        0.829,      2914.014 ),
  new VSOP87Coefficient( 526,        5.383,      3738.761 ),
  new VSOP87Coefficient( 473,        5.199,      3127.313 ),
  new VSOP87Coefficient( 348,        4.832,      16703.062 ),
  new VSOP87Coefficient( 284,        2.907,      3532.061 ),
  new VSOP87Coefficient( 280,        5.257,      6283.076 ),
  new VSOP87Coefficient( 276,        1.218,      6254.627 ),
  new VSOP87Coefficient( 275,        2.908,      1748.016 ),
  new VSOP87Coefficient( 270,        3.764,      5884.927 ),
  new VSOP87Coefficient( 239,        2.037,      1194.447 ),
  new VSOP87Coefficient( 234,        5.105,      5486.778 ),
  new VSOP87Coefficient( 228,        3.255,      6872.673 ),
  new VSOP87Coefficient( 223,        4.199,      3149.164 ),
  new VSOP87Coefficient( 219,        5.583,      191.448 ),
  new VSOP87Coefficient( 208,        5.255,      3340.545 ),
  new VSOP87Coefficient( 208,        4.846,      3340.680 ),
  new VSOP87Coefficient( 186,        5.699,      6677.702 ),
  new VSOP87Coefficient( 183,        5.081,      6684.748 ),
  new VSOP87Coefficient( 179,        4.184,      3333.499 ),
  new VSOP87Coefficient( 176,        5.953,      3870.303 ),
  new VSOP87Coefficient( 164,        3.799,      4136.910 )
};

static readonly VSOP87Coefficient[]  g_R1MarsCoefficients =
{ 
  new VSOP87Coefficient( 1107433,  2.0325052,  3340.6124267 ),
  new VSOP87Coefficient( 103176,   2.370718,   6681.224853 ),
  new VSOP87Coefficient( 12877,    0,          0 ),
  new VSOP87Coefficient( 10816,    2.70888,    10021.83728 ),
  new VSOP87Coefficient( 1195,     3.0470,     13362.4497 ),
  new VSOP87Coefficient( 439,      2.888,      2281.230 ),
  new VSOP87Coefficient( 396,      3.423,      3344.136 ),
  new VSOP87Coefficient( 183,      1.584,      2544.314 ),
  new VSOP87Coefficient( 136,      3.385,      16703.062 ),
  new VSOP87Coefficient( 128,      6.043,      3337.089 ),
  new VSOP87Coefficient( 128,      0.630,      1059.382 ),
  new VSOP87Coefficient( 127,      1.954,      796.298 ),
  new VSOP87Coefficient( 118,      2.998,      2146.165 ),
  new VSOP87Coefficient( 88,       3.42,       398.15 ),
  new VSOP87Coefficient( 83,       3.86,       3738.76 ),
  new VSOP87Coefficient( 76,       4.45,       6151.53 ),
  new VSOP87Coefficient( 72,       2.76,       529.69 ),
  new VSOP87Coefficient( 67,       2.55,       1751.54 ),
  new VSOP87Coefficient( 66,       4.41,       1748.02 ),
  new VSOP87Coefficient( 58,       0.54,       1194.45 ),
  new VSOP87Coefficient( 54,       0.68,       8962.46 ),
  new VSOP87Coefficient( 51,       3.73,       6684.75 ),
  new VSOP87Coefficient( 49,       5.73,       3340.60 ),
  new VSOP87Coefficient( 49,       1.48,       3340.63 ),
  new VSOP87Coefficient( 48,       2.58,       3149.16 ),
  new VSOP87Coefficient( 48,       2.29,       2914.01 ),
  new VSOP87Coefficient( 39,       2.32,       4136.91 )
};

static readonly VSOP87Coefficient[]  g_R2MarsCoefficients =
{ 
  new VSOP87Coefficient( 44242,  0.47931,  3340.61243 ),
  new VSOP87Coefficient( 8138,   0.8700,   6681.2249 ),
  new VSOP87Coefficient( 1275,   1.2259,   10021.8373 ),
  new VSOP87Coefficient( 187,    1.573,    13362.450 ),
  new VSOP87Coefficient( 52,     3.14,     0 ),
  new VSOP87Coefficient( 41,     1.97,     3344.14 ),
  new VSOP87Coefficient( 27,     1.92,     16703.06 ),
  new VSOP87Coefficient( 18,     4.43,     2281.23 ),
  new VSOP87Coefficient( 12,     4.53,     3185.19 ),
  new VSOP87Coefficient( 10,     5.39,     1059.38 ),
  new VSOP87Coefficient( 10,     0.42,     796.30 )
};

static readonly VSOP87Coefficient[]  g_R3MarsCoefficients =
{ 
  new VSOP87Coefficient( 1113, 5.1499, 3340.6124 ),
  new VSOP87Coefficient( 424,  5.613,  6681.225 ),
  new VSOP87Coefficient( 100,  5.997,  10021.837 ),
  new VSOP87Coefficient( 20,   0.08,   13362.45 ),
  new VSOP87Coefficient( 5,    3.14,   0 ),
  new VSOP87Coefficient( 3,    0.43,   16703.06 )
};

static readonly VSOP87Coefficient[]  g_R4MarsCoefficients =
{ 
  new VSOP87Coefficient( 20, 3.58, 3340.61 ),
  new VSOP87Coefficient( 16, 4.05, 6681.22 ),
  new VSOP87Coefficient( 6,  4.46, 10021.84 ),
  new VSOP87Coefficient( 2,  4.84, 13362.45 )
};



///////////////////////////// Implementation //////////////////////////////////

public static double EclipticLongitude(double JD)
{
  double rho = (JD - 2451545) / 365250;
  double rhosquared = rho*rho;
  double rhocubed = rhosquared*rho;
  double rho4 = rhocubed*rho;
  double rho5 = rho4*rho;

  //Calculate L0
  double L0 = 0;
  int i;
  for (i = 0; i < g_L0MarsCoefficients.Length; i++)
    L0 += g_L0MarsCoefficients[i].A * Math.Cos(g_L0MarsCoefficients[i].B + g_L0MarsCoefficients[i].C*rho);

  //Calculate L1
  double L1 = 0;
  for (i = 0; i < g_L1MarsCoefficients.Length; i++)
    L1 += g_L1MarsCoefficients[i].A * Math.Cos(g_L1MarsCoefficients[i].B + g_L1MarsCoefficients[i].C*rho);

  //Calculate L2
  double L2 = 0;
  for (i = 0; i < g_L2MarsCoefficients.Length; i++)
    L2 += g_L2MarsCoefficients[i].A * Math.Cos(g_L2MarsCoefficients[i].B + g_L2MarsCoefficients[i].C*rho);

  //Calculate L3
  double L3 = 0;
  for (i = 0; i < g_L3MarsCoefficients.Length; i++)
    L3 += g_L3MarsCoefficients[i].A * Math.Cos(g_L3MarsCoefficients[i].B + g_L3MarsCoefficients[i].C*rho);

  //Calculate L4
  double L4 = 0;
  for (i = 0; i < g_L4MarsCoefficients.Length; i++)
    L4 += g_L4MarsCoefficients[i].A * Math.Cos(g_L4MarsCoefficients[i].B + g_L4MarsCoefficients[i].C*rho);

  //Calculate L5
  double L5 = 0;
  for (i = 0; i < g_L5MarsCoefficients.Length; i++)
    L5 += g_L5MarsCoefficients[i].A * Math.Cos(g_L5MarsCoefficients[i].B + g_L5MarsCoefficients[i].C*rho);

  double value = (L0 + L1*rho + L2*rhosquared + L3*rhocubed + L4*rho4 + L5*rho5) / 100000000;

  //convert results back to degrees
  value = CoordinateTransformation.MapTo0To360Range(CoordinateTransformation.RadiansToDegrees(value));
  return value;
}

public static double EclipticLatitude(double JD)
{
  double rho = (JD - 2451545) / 365250;
  double rhosquared = rho*rho;
  double rhocubed = rhosquared*rho;
  double rho4 = rhocubed*rho;

  //Calculate B0
  double B0 = 0;
  int i;
  for (i = 0; i < g_B0MarsCoefficients.Length; i++)
    B0 += g_B0MarsCoefficients[i].A * Math.Cos(g_B0MarsCoefficients[i].B + g_B0MarsCoefficients[i].C*rho);

  //Calculate B1
  double B1 = 0;
  for (i = 0; i < g_B1MarsCoefficients.Length; i++)
    B1 += g_B1MarsCoefficients[i].A * Math.Cos(g_B1MarsCoefficients[i].B + g_B1MarsCoefficients[i].C*rho);

  //Calculate B2
  double B2 = 0;
  for (i = 0; i < g_B2MarsCoefficients.Length; i++)
    B2 += g_B2MarsCoefficients[i].A * Math.Cos(g_B2MarsCoefficients[i].B + g_B2MarsCoefficients[i].C*rho);

  //Calculate B3
  double B3 = 0;
  for (i = 0; i < g_B3MarsCoefficients.Length; i++)
    B3 += g_B3MarsCoefficients[i].A * Math.Cos(g_B3MarsCoefficients[i].B + g_B3MarsCoefficients[i].C*rho);

  //Calculate B4
  double B4 = 0;
  for (i = 0; i < g_B4MarsCoefficients.Length; i++)
    B4 += g_B4MarsCoefficients[i].A * Math.Cos(g_B4MarsCoefficients[i].B + g_B4MarsCoefficients[i].C*rho);

  double value = (B0 + B1*rho + B2*rhosquared + B3*rhocubed + B4*rho4) / 100000000;

  //convert results back to degrees
  value = CoordinateTransformation.RadiansToDegrees(value);
  return value;
}

public static double RadiusVector(double JD)
{
  double rho = (JD - 2451545) / 365250;
  double rhosquared = rho*rho;
  double rhocubed = rhosquared*rho;
  double rho4 = rhocubed*rho;

  //Calculate R0
  double R0 = 0;
  int i;
  for (i = 0; i < g_R0MarsCoefficients.Length; i++)
    R0 += g_R0MarsCoefficients[i].A * Math.Cos(g_R0MarsCoefficients[i].B + g_R0MarsCoefficients[i].C*rho);

  //Calculate R1
  double R1 = 0;
  for (i = 0; i < g_R1MarsCoefficients.Length; i++)
    R1 += g_R1MarsCoefficients[i].A * Math.Cos(g_R1MarsCoefficients[i].B + g_R1MarsCoefficients[i].C*rho);

  //Calculate R2
  double R2 = 0;
  for (i = 0; i < g_R2MarsCoefficients.Length; i++)
    R2 += g_R2MarsCoefficients[i].A * Math.Cos(g_R2MarsCoefficients[i].B + g_R2MarsCoefficients[i].C*rho);

  //Calculate R3
  double R3 = 0;
  for (i = 0; i < g_R3MarsCoefficients.Length; i++)
    R3 += g_R3MarsCoefficients[i].A * Math.Cos(g_R3MarsCoefficients[i].B + g_R3MarsCoefficients[i].C*rho);

  //Calculate R4
  double R4 = 0;
  for (i = 0; i < g_R4MarsCoefficients.Length; i++)
    R4 += g_R4MarsCoefficients[i].A * Math.Cos(g_R4MarsCoefficients[i].B + g_R4MarsCoefficients[i].C*rho);

  return (R0 + R1*rho + R2*rhosquared + R3*rhocubed + R4*rho4) / 100000000;
}
    }
}
